package com.ext.aic.handler;

import android.app.Notification;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;

import com.ext.aic.AICApplication;
import com.ext.aic.AICManager;
import com.ext.aic.beans.box.NotificationMessage;
import com.ext.aic.constants.CacheConstants;
import com.ext.aic.utils.BoxUtils;
import com.ext.aic.utils.RedirectUtils;
import com.eyinfo.mpkv.MPLocalKV;

public class NotificationHandler {

    public void receiveNotification(Notification notification) {
        Bundle extras = notification.extras;
        String user = extras.getString(Notification.EXTRA_TITLE);
        String text = extras.getString(Notification.EXTRA_TEXT);
        String subText = extras.getString(Notification.EXTRA_SUB_TEXT);
        if (TextUtils.isEmpty(user)) {
            return;
        }
        if (TextUtils.isEmpty(text)) {
            text = subText;
        }
        MPLocalKV.getInstance().put(CacheConstants.PROCESSED_NOTIFY_STATUS_KEY, false);
        NotificationMessage notifyMessage = BoxUtils.getInstance().getNotifyMessage(user, text);
        String unique = BoxUtils.getInstance().getNotifyUnique(notifyMessage);
        if (BoxUtils.getInstance().existsNotifyUnique(unique)) {
            return;
        }
        AICManager.getInstance().insertOrUpdate(NotificationMessage.class, notifyMessage);
        BoxUtils.getInstance().saveNotifyUnique(unique);

        //此时微信在后台或未启动，启动微信客户端
        Context applicationContext = AICApplication.getInstance().getApplicationContext();
        RedirectUtils.getInstance().startWeChat(applicationContext);
    }
}
